<template>
  <div class="setting">
    <el-card>
      <el-tabs>
        <el-tab-pane label="角色管理">
          <el-button type="primary" @click="addEvent">新增角色</el-button>
          <el-table class="table" :data="list" border>
            <el-table-column align="center" label="序号" width="80">
              <!-- 插槽 -->
              <template v-slot="{ $index }">
                <div>
                  {{ (page.page - 1) * page.pagesize + $index + 1 }}
                </div>
              </template>
            </el-table-column>
            <el-table-column align="center" label="名称" prop="name" />
            <el-table-column align="center" label="描述" prop="description" />
            <el-table-column align="center" label="操作">
              <template v-slot="{ row }">
                <el-button
                  type="success"
                  @click="setPerEvent(row.id)"
                >分配权限</el-button>
                <el-button
                  type="primary"
                  @click="editEvent(row)"
                >编辑</el-button>
                <el-button
                  type="danger"
                  @click="delEvent(row.id)"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <div class="page">
            <el-pagination
              :current-page.sync="page.page"
              :page-sizes="[1, 3, 5, 10]"
              :page-size.sync="page.pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="5"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </div>
        </el-tab-pane>
        <el-tab-pane label="公司信息" lazy>
          <Info />
        </el-tab-pane>
      </el-tabs>
    </el-card>
    <Add ref="add" @getData="getData" />
    <SetPermission ref="per" />
  </div>
</template>

<script>
import { sysRole, sysRoleDelete } from '@/api/setting'
import Add from './components/Add.vue'
import Info from './components/Info.vue'
import SetPermission from './components/SetPermission.vue'
export default {
  components: {
    Add,
    Info,
    SetPermission
  },
  data() {
    return {
      list: [],
      page: {
        page: 1,
        pagesize: +localStorage.getItem('setting_pagesize') || 10 // 持久化保证更好的用户体验，字符串转化成数字
      },
      total: 100
    }
  },
  mounted() {
    this.getData()
  },
  methods: {
    // 初始化/刷新页面
    async getData() {
      const res = await sysRole(this.page)
      this.list = res.data.rows
      this.total = res.data.total
    },
    // 页码改变
    handleCurrentChange() {
      this.getData()
    },
    // 页容量改变，持久化保证更好的用户体验，数字转化成字符串
    handleSizeChange() {
      localStorage.setItem('setting_pagesize', this.page.pagesize + '')
      this.getData()
    },
    addEvent() {
      this.$refs.add.show = true
      this.$refs.add.mode = 'add'
    },
    editEvent(row) {
      this.$refs.add.show = true
      this.$refs.add.mode = 'edit'
      this.$refs.add.form = JSON.parse(JSON.stringify(row))
    },
    async delEvent(id) {
      await this.$confirm('您确定删除该项吗？', '温馨提示')
      await sysRoleDelete(id)
      this.$message.success('删除成功')
      if (this.page.page > 1 && this.list.length === 1) {
        this.page.page--
      }
      this.getData()
    },
    // 分配权限点击事件
    setPerEvent(id) {
      this.$refs.per.show = true
      this.$refs.per.getPerList()
      this.$refs.per.getRoleDetail(id)
    }
  }
}
</script>

<style lang="scss" scoped>
.setting {
  padding: 20px;
  .table {
    margin-top: 15px;
  }
  .page {
    padding-top: 20px;
    text-align: center;
  }
}
</style>
